package com.cloud.job.util;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.cloud.job.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Component
public class OssHelper {

    @Autowired
    Config config;
    static Map<String, String> contentTypeMap;

    //根据自己上传文件类型可自行添加
    static {
        contentTypeMap = new HashMap<>();
        contentTypeMap.put("html", "text/html");
        contentTypeMap.put("htm", "text/html");
        contentTypeMap.put("js", "application/x-javascript");
        contentTypeMap.put("css", "text/css");
        contentTypeMap.put("gif", "image/gif");
        contentTypeMap.put("jpg", "image/jpeg");
        contentTypeMap.put("jpeg", "image/jpeg");
        contentTypeMap.put("png", "image/png");
    }

    /**
     * 上传单个文件到OSS
     *
     * @param file    要上传的文件File对象
     * @param objName 上传后的文件名，包含文件夹，比如 game/game/test.txt
     * @return
     */
    public boolean uploadFileToOss(File file, String objName) {
        try {
            OSSClient ossClient = null;
            try {
//                ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecert);
                ossClient = new OSSClient(config.getOssEndpoint(), config.getOssAccessKeyId(), config.getOssAccessKeySecret());
            } catch (Exception e) {
                e.printStackTrace();
            }
            ObjectMetadata meta = new ObjectMetadata();
            String suffixName = getFileSuffixName(objName);
            String contentType = contentTypeMap.get(suffixName);
            if (contentType == null) {
                meta.setContentType("text/plain");
            } else {
                meta.setContentType(contentType);
            }
            System.out.println("--" + contentType);
            ossClient.putObject(config.getOssBucketName(), objName, file, meta);
            ossClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String getFileSuffixName(String objName) {
        if (StrUtil.isEmpty(objName)) {
            return "";
        }
        return StrUtil.subAfter(objName, ".", true);
    }

    /**
     * 递归文件目录上传
     *
     * @param sourceFile 本地文件目录
     * @param ossSource  oss存储路径
     * @return
     */
    public boolean uploadDirsToOss(File sourceFile, String ossSource) {
        if (!sourceFile.exists()) {
//            logger.error("上传文件目录不存:"+sourceFile.getPath());
            return false;
        }
        File[] files = sourceFile.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    uploadFileToOss(file, ossSource + "/" + file.getName());
                } else if (file.isDirectory()) {
                    uploadDirsToOss(file, ossSource + "/" + file.getName());
                }
            }
        }
        return true;
    }
}
